package com.libl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libl.mapper.DtsGoodsMapper;
import com.libl.pojo.DtsGoods;
import com.libl.pojo.DtsGoodsAttribute;
import com.libl.pojo.DtsGoodsProduct;
import com.libl.pojo.DtsGoodsSpecification;
import com.libl.service.DtsGoodsAttributeService;
import com.libl.service.DtsGoodsProductService;
import com.libl.service.DtsGoodsService;
import com.libl.service.DtsGoodsSpecificationService;
import com.libl.vo.GoodsAllinone;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    DtsGoodsMapper goodsMapper;

    @Override
    public int getTotal() {
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getDeleted, false);

        Integer count = goodsMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String goodsSn, String name, String sort, String order, List<Integer> brandIds) {
        //创建mybatis-plus的page对象
        IPage<DtsGoods> ipage = new Page<>(page, limit);
        //设置查询条件
        QueryWrapper<DtsGoods> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(goodsSn)) {
            wrapper.like("goods_sn",goodsSn);
        }
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        //sort和order都不为空
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if("desc".equals(order)){
                //降序
                wrapper.orderByDesc(sort);
            }else{
                //升序
                wrapper.orderByAsc(sort);
            }
        }
        //如果有品牌id
        if(brandIds != null && brandIds.size() > 0){
            //注意：brandIds=?  这里的?是多个数据，使用in查询
            wrapper.in("brand_id",brandIds);
        }
        goodsMapper.selectPage(ipage,wrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());
        //把查询到的数据返回
        return data;
    }

    @Override
    public DtsGoods selectById(Integer id) {
        DtsGoods goods = goodsMapper.selectById(id);
        return goods;
    }

    @Autowired
    DtsGoodsAttributeService attributeService;
    @Autowired
    DtsGoodsSpecificationService specificationService;
    @Autowired
    DtsGoodsProductService productService;

    @Override
    public void update(GoodsAllinone goodsAllinone) {
        //获取商品信息
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setUpdateTime(new Date());

        //更新商品数据
        goodsMapper.updateById(goods);
        //更新商品的属性
        //先删除该商品的属性
        attributeService.deleteByGoodsId(goods.getId());
        //再新增商品的属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes != null && attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                //设置商品id
                attribute.setGoodsId(goods.getId());
                attributeService.insert(attribute);
            }
        }
        //更新商品的规格
        //先删除该商品的规格
        specificationService.deleteByGoodsId(goods.getId());
        //再新增商品的规格
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications != null && specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                //设置商品id
                specification.setGoodsId(goods.getId());
                specificationService.insert(specification);
            }
        }
        //更新商品的库存
        //先删除该商品的库存，防止有其他关联
        productService.deleteByGoodsId(goods.getId());
        //再新增商品的库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if(products != null && products.length > 0){
            for (DtsGoodsProduct product : products) {
                product.setGoodsId(goods.getId());
                productService.insert(product);
            }
        }
    }

    @Override
    public void save(GoodsAllinone goodsAllinone) {
        //保存商品信息
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);

        goodsMapper.insert(goods);

        //新增商品的属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes != null && attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                //设置商品id
                attribute.setGoodsId(goods.getId());
                attributeService.insert(attribute);
            }
        }
        //新增商品的规格
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications != null && specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                //设置商品id
                specification.setGoodsId(goods.getId());
                specificationService.insert(specification);
            }
        }
        //新增商品的库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if(products != null && products.length > 0){
            for (DtsGoodsProduct product : products) {
                product.setGoodsId(goods.getId());
                productService.insert(product);
            }
        }
    }

    /**
     * 商品跟属性，规格，库存，订单有关系
     * 进行逻辑删除
     * 设置商品 deleted为true
     * 设置商品属性 deleted为true
     * 设置商品规格 deleted为true
     * 设置商品库存 deleted为true
     * @param goods
     */
    @Override
    public void delete(DtsGoods goods) {
        //根据id修改商品的属性、规格、库存，将它们修改为逻辑删除
        attributeService.updateByGoodsId(goods.getId());
        specificationService.updateByGoodsId(goods.getId());
        productService.updateByGoodsId(goods.getId());
        //设置为逻辑删除
        goods.setDeleted(true);
        goodsMapper.updateById(goods);
    }
}
